package lingou.homework.postbar.controller.proto;

import com.fasterxml.jackson.databind.JsonNode;
import lingou.homework.postbar.controller.tool.JsonHelper;
import lingou.homework.postbar.service.enums.MessageOpEnum;
import lingou.homework.postbar.service.enums.UserTypeEnum;
import lingou.homework.postbar.service.PostService;
import lingou.homework.postbar.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

@Component
public class JsonMapper {
    @Autowired
    UserService userService;
    @Autowired
    PostService postService;

    private Map<String, Function<JsonNode, JsonHelper.Entry>> all;
    private Map<String, Function<JsonNode, JsonHelper.Entry>> getAllMapper() {
        if(all == null){
            all = new HashMap<>();
            all.put("postId", x -> JsonHelper.of("post", postService.getPost(x.asLong())));
            all.put("userId", x -> JsonHelper.of("user", userService.getUser(x.asLong())));
            all.put("userType", x -> JsonHelper.of("userType", UserTypeEnum.select(x.asLong())));
            all.put("msgOp", x -> JsonHelper.of("msgOp", MessageOpEnum.select(x.asLong())));
        }
        return all;
    }

    public Map.Entry<String, Function<JsonNode, JsonHelper.Entry>> getMapper(String id){
        return Map.entry(id, getAllMapper().get(id));
    }
}
